% fname: routing_xy__topology_8x8__.m
% ./noxim -routing xy -dimx 8 -dimy 8  -sim 10000 -warmup 2000 -size 8 8 -buffer 4 -hs 6 0.2 -hs 7 0.2 -hs 14 0.2 -hs 15 0.2 

function [max_pir, max_throughput, min_delay] = routing_xy__topology_8x8__(symbol)

data = [
%             pir      avg_delay     throughput      max_delay       rpackets         rflits
           0.0015         22.887      0.0138879            109            805           6444
           0.0015        23.2565      0.0141579            195            807           6456
           0.0015        23.0833      0.0136504            268            804           6443
           0.0015        21.3902         0.0123            101            738           5904
           0.0015        20.4178      0.0127733            106            754           6029
           0.0015         20.113      0.0131293             93            761           6092
           0.0015        22.0037      0.0131373            150            801           6411
           0.0015        20.2337       0.012541             97            766           6120
           0.0015        22.5498      0.0136377            169            804           6437
           0.0015        21.7343      0.0127766            123            779           6235
           0.0015        21.2368      0.0131121            170            760           6084
           0.0015        23.0038      0.0130104            142            782           6245
           0.0015        19.8464       0.011925            104            716           5724
           0.0015        21.0977      0.0128589             85            798           6378
           0.0015        24.8325      0.0136294            181            776           6215
           0.0015        22.3045      0.0128832            119            785           6287
           0.0015        23.0076      0.0127964            138            793           6347
           0.0015        20.7748      0.0123146            125            737           5911
           0.0015        23.1022      0.0135146            139            812           6487
           0.0015        20.8566      0.0122083            160            732           5860
           0.0017        24.9081       0.013879            234            860           6884
           0.0017        26.0762      0.0148246            209            919           7353
           0.0017        23.8086      0.0140198            125            883           7066
           0.0017         24.901      0.0142659            185            899           7190
           0.0017        24.1738      0.0146123            145            863           6897
           0.0017        23.3686      0.0149012            130            925           7391
           0.0017        27.5458      0.0149298            257            852           6808
           0.0017        23.4643      0.0135302            190            840           6711
           0.0017        22.4879      0.0146674            111            865           6923
           0.0017        26.1902      0.0146071            177            920           7362
           0.0017        25.5668      0.0147242            147            928           7421
           0.0017        21.3329      0.0146144            128            862           6898
           0.0017        25.3614       0.014207            199            866           6933
           0.0017         23.489      0.0159649            161            912           7280
           0.0017        21.8744      0.0142722            109            884           7079
           0.0017        21.7919      0.0138488            116            860           6869
           0.0017         21.991      0.0140635            117            885           7088
           0.0017        22.5468      0.0149203            131            865           6923
           0.0017        21.5501      0.0150351            126            858           6856
           0.0017        22.9491      0.0137458            207            825           6598
           0.0019        23.8842      0.0161144            153            950           7606
           0.0019        24.8596      0.0158178            127            933           7466
           0.0019        28.5135       0.016125            263           1001           7998
           0.0019        25.7724      0.0156042            161            936           7490
           0.0019         28.406      0.0151309            359            968           7747
           0.0019        26.9949      0.0158185            150            981           7846
           0.0019        29.2954      0.0153887            428            985           7879
           0.0019        27.0208      0.0160615            227           1012           8095
           0.0019        22.2028      0.0158814            109            937           7496
           0.0019        25.5658      0.0149563            192            942           7538
           0.0019        27.1672      0.0162157            140           1005           8043
           0.0019        31.1183      0.0161844            252            989           7898
           0.0019        25.1636      0.0154375            203            990           7904
           0.0019        26.0297      0.0160357            168           1009           8082
           0.0019        26.4911      0.0166107            230           1012           8106
           0.0019        24.8652      0.0152222            209            957           7672
           0.0019        25.7243      0.0154306            139            972           7777
           0.0019        25.9809      0.0157698            189            994           7948
           0.0019        27.6866      0.0164313            244            986           7887
           0.0019        25.3848      0.0161815            159           1003           8026
           0.0021        26.4385       0.016791            145           1024           8194
           0.0021        27.8378      0.0164325            141           1036           8282
           0.0021        29.2584      0.0169306            196           1068           8533
           0.0021        30.8194      0.0175469            367           1124           8984
           0.0021        26.4062      0.0168333            192           1061           8484
           0.0021         28.025      0.0173125            203           1039           8310
           0.0021        27.0125      0.0165417            161           1043           8337
           0.0021        33.2344      0.0175605            304           1088           8710
           0.0021        30.8228      0.0165594            512           1010           8081
           0.0021        27.9361       0.015879            251            986           7876
           0.0021        28.0441      0.0169883            187           1088           8698
           0.0021        32.0221      0.0162578            246           1039           8324
           0.0021        28.1117      0.0173327            208           1074           8597
           0.0021        27.7066      0.0165323            191           1026           8200
           0.0021        35.7203      0.0183367            315           1137           9095
           0.0021        30.5974      0.0172339            194           1068           8548
           0.0021        28.8636      0.0165198            185           1041           8326
           0.0021        36.5693      0.0169023            524           1082           8654
           0.0021        27.6654      0.0166309            133           1064           8515
           0.0021        33.6664      0.0175714            325           1106           8856
           0.0023        48.3005      0.0186885            498           1178           9419
           0.0023        41.9456      0.0189107            439           1194           9531
           0.0023        31.8695      0.0187944            221           1165           9322
           0.0023        36.5041      0.0194425            291           1226           9799
           0.0023        50.4167      0.0190488            843           1219           9753
           0.0023         35.381      0.0194604            313           1168           9341
           0.0023         36.215      0.0188197            306           1149           9184
           0.0023        34.9416      0.0184766            272           1182           9460
           0.0023        35.4794       0.019541            378           1191           9536
           0.0023        39.9406      0.0192679            322           1213           9711
           0.0023        32.9522      0.0186211            373           1192           9534
           0.0023        61.8972      0.0196069           1087           1216           9725
           0.0023        38.9624      0.0195508            333           1250          10010
           0.0023         47.927      0.0203083            769           1219           9748
           0.0023        35.0372      0.0181935            215           1128           9024
           0.0023        46.7757      0.0204254            348           1266          10131
           0.0023        40.6621      0.0192578            644           1231           9860
           0.0023        34.4952      0.0183024            231           1135           9078
           0.0023        32.2709      0.0185323            323           1148           9192
           0.0023        32.1283      0.0191169            451           1185           9482
           0.0025        51.4824      0.0204727            508           1310          10482
           0.0025        54.1065      0.0198254           1077           1249           9992
           0.0025        44.0594      0.0197129            428           1263          10093
           0.0025        55.8111      0.0203246            894           1260          10081
           0.0025        41.2839       0.020129            429           1268          10145
           0.0025         51.153      0.0211607            779           1333          10665
           0.0025        49.5487      0.0198594            557           1272          10168
           0.0025        42.4972      0.0199147            469           1255          10037
           0.0025        54.0906      0.0201875            647           1292          10336
           0.0025        42.1629      0.0185176            376           1185           9481
           0.0025        43.0873      0.0210208            389           1260          10090
           0.0025        74.4099      0.0211349           1321           1332          10652
           0.0025        44.5552      0.0210758            509           1286          10285
           0.0025         93.395      0.0211211           2131           1352          10814
           0.0025        71.1427      0.0201426           1175           1289          10313
           0.0025        42.3694      0.0205098            478           1313          10501
           0.0025        81.5228       0.021209            898           1360          10859
           0.0025         52.183      0.0207383            720           1328          10618
           0.0025        38.9171      0.0186431            470           1158           9247
           0.0025        43.4828       0.020254            777           1276          10208
           0.0027        105.739      0.0228367           2428           1415          11327
           0.0027         111.93      0.0227051           1714           1453          11625
           0.0027         56.635      0.0200957            774           1285          10289
           0.0027        66.5154      0.0217871            636           1393          11155
           0.0027        50.5904      0.0212441            671           1360          10877
           0.0027        60.3213      0.0216151            714           1360          10894
           0.0027        53.2965       0.021248            389           1359          10879
           0.0027        121.343       0.022002           2371           1385          11089
           0.0027        78.8366      0.0209297           1814           1340          10716
           0.0027        108.968      0.0219023           2143           1403          11214
           0.0027        131.313      0.0229385           2615           1445          11561
           0.0027        51.0595      0.0208016            658           1310          10484
           0.0027        86.3352      0.0227117           1004           1408          11265
           0.0027        68.5888      0.0212758           2097           1340          10723
           0.0027        60.1597       0.021834            519           1396          11179
           0.0027        107.414      0.0216484           1566           1385          11084
           0.0027        80.9768      0.0208594           1323           1334          10680
           0.0027        64.7649      0.0210694            900           1327          10619
           0.0027        114.522      0.0221445           1482           1417          11338
           0.0027        84.2596      0.0208223            870           1333          10661
           0.0029        135.208       0.022416           2346           1434          11477
           0.0029        304.767      0.0226445           6235           1447          11594
           0.0029        222.327      0.0237282           4623           1496          11959
           0.0029        182.237      0.0223105           3738           1429          11423
           0.0029        312.402       0.022625           5947           1402          11222
           0.0029         308.64      0.0227183           7086           1433          11450
           0.0029         111.47      0.0230141           2007           1427          11415
           0.0029        173.043      0.0217754           3197           1394          11149
           0.0029        239.237      0.0228066           4765           1460          11677
           0.0029         206.59      0.0228691           2456           1463          11709
           0.0029        172.409      0.0224258           4100           1434          11482
           0.0029        366.519      0.0223398           6301           1429          11438
           0.0029        210.973      0.0226391           6030           1404          11229
           0.0029         96.557       0.022875           1441           1465          11712
           0.0029        238.468      0.0228027           5740           1461          11675
           0.0029        303.158       0.022457           8418           1436          11498
           0.0029        300.557      0.0229219           5583           1468          11736
           0.0029        199.015      0.0223926           2847           1434          11465
           0.0029        374.582       0.022918           7103           1398          11184
           0.0029        86.1088      0.0229824           1045           1471          11767
           0.0031        332.455      0.0235547           8473           1508          12060
           0.0031        389.397      0.0232004           7076           1462          11693
           0.0031        267.114      0.0223887           5707           1433          11463
           0.0031        401.128      0.0230801           5370           1477          11817
           0.0031        231.762      0.0233672           4854           1497          11964
           0.0031        267.847      0.0232031           4926           1487          11880
           0.0031        432.788      0.0230918           7230           1478          11823
           0.0031        246.807      0.0232305           3920           1487          11894
           0.0031        310.932      0.0229583           7046           1447          11571
           0.0031        208.584      0.0234863           4102           1504          12025
           0.0031        255.418      0.0244037           6062           1490          11909
           0.0031        359.124      0.0224199           7187           1435          11479
           0.0031        138.038      0.0233301           2831           1492          11945
           0.0031         292.34      0.0229375           7281           1469          11744
           0.0031        282.552      0.0241973           7357           1550          12389
           0.0031        219.929      0.0234961           5457           1503          12030
           0.0031        286.126      0.0240143           6708           1464          11719
           0.0031        304.029      0.0234941           6215           1502          12029
           0.0031        323.212      0.0222659           6078           1403          11222
           0.0031        207.568      0.0231953           3408           1484          11876
           0.0033        345.626      0.0240746           6670           1493          11941
           0.0033        373.084      0.0236797           7342           1515          12124
           0.0033        473.114      0.0229187           6525           1444          11551
           0.0033        556.074      0.0235417           7363           1485          11865
           0.0033        344.487      0.0241348           6167           1545          12357
           0.0033        437.338      0.0238849           6449           1504          12038
           0.0033        409.569      0.0229668           5873           1469          11759
           0.0033        382.498      0.0243115           4738           1531          12253
           0.0033        430.709      0.0243398           4805           1558          12462
           0.0033        432.638      0.0233869           8579           1475          11787
           0.0033        467.312      0.0227969           7155           1458          11672
           0.0033        412.935      0.0230081           7074           1426          11412
           0.0033        353.478      0.0240859           6494           1541          12332
           0.0033        425.193      0.0234434           7258           1499          12003
           0.0033        514.446      0.0239246           6802           1506          12058
           0.0033        448.641      0.0236289           6306           1511          12098
           0.0033        450.747      0.0240734           8164           1516          12133
           0.0033        411.238      0.0239623           6441           1511          12077
           0.0033        396.251      0.0232051           5808           1486          11881
           0.0033        502.012      0.0234688           6650           1501          12016
];

rows = size(data, 1);
cols = size(data, 2);

data_delay = [];
for i = 1:rows/20,
   ifirst = (i - 1) * 20 + 1;
   ilast  = ifirst + 20 - 1;
   tmp = data(ifirst:ilast, cols-5+1);
   avg = mean(tmp);
   [h sig ci] = ttest(tmp, 0.1);
   ci = (ci(2)-ci(1))/2;
   data_delay = [data_delay; data(ifirst, 1:cols-5), avg ci];
end

figure(1);
hold on;
plot(data_delay(:,1), data_delay(:,2), symbol);

data_throughput = [];
for i = 1:rows/20,
   ifirst = (i - 1) * 20 + 1;
   ilast  = ifirst + 20 - 1;
   tmp = data(ifirst:ilast, cols-5+2);
   avg = mean(tmp);
   [h sig ci] = ttest(tmp, 0.1);
   ci = (ci(2)-ci(1))/2;
   data_throughput = [data_throughput; data(ifirst, 1:cols-5), avg ci];
end

figure(2);
hold on;
plot(data_throughput(:,1), data_throughput(:,2), symbol);

data_maxdelay = [];
for i = 1:rows/20,
   ifirst = (i - 1) * 20 + 1;
   ilast  = ifirst + 20 - 1;
   tmp = data(ifirst:ilast, cols-5+3);
   avg = mean(tmp);
   [h sig ci] = ttest(tmp, 0.1);
   ci = (ci(2)-ci(1))/2;
   data_maxdelay = [data_maxdelay; data(ifirst, 1:cols-5), avg ci];
end

figure(3);
hold on;
plot(data_maxdelay(:,1), data_maxdelay(:,2), symbol);


%-------- Saturation Analysis -----------
slope=[];
for i=2:size(data_throughput,1),
    slope(i-1) = (data_throughput(i,2)-data_throughput(i-1,2))/(data_throughput(i,1)-data_throughput(i-1,1));
end

for i=2:size(slope,2),
    if slope(i) < (0.95*mean(slope(1:i)))
        max_pir = data_throughput(i, 1);
        max_throughput = data_throughput(i, 2);
        min_delay = data_delay(i, 2);
        break;
    end
end
